package com.CityHunter.Request;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import android.location.Location;
import android.util.Log;

import com.CityHunter.Common.Http;

public class UpdateLocationRequest {

	private String updateLocationPage;
	private int userid;
	
	public UpdateLocationRequest(String updateLocationPage,int userid){
		this.updateLocationPage = updateLocationPage;
		this.userid = userid;
	}
	
	public int updateLocation(Location currentLocation){
		
		int result = 0;
		
		double geoLatitude = currentLocation.getLatitude();
		double geoLongitude = currentLocation.getLongitude();
		
		// Declare NameValuePair to store the data
		ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("latitude", Double.toString(geoLatitude)));
		params.add(new BasicNameValuePair("longitude", Double.toString(geoLongitude)));
		params.add(new BasicNameValuePair("userid", Integer.toString(userid)));

		Http http = new Http();
		Log.d("abdy_Debug", "Sending Request : " + updateLocationPage);
		
		JSONArray jsonResponse = http.sendRequest(updateLocationPage, params);
		try {
			result = Integer.parseInt(jsonResponse.getJSONObject(0)
					.getString("result"));
			Log.d("Sttp_Debug", "Update Location Result: " + result);
		} catch (Exception e) {
			Log.e("Sttp_Error", e.getMessage().toString());
		}
		
		return result;
	}
}
